package com.itstherules.jmsunit.expectation;

import java.util.LinkedHashMap;
import java.util.Map;

import com.itstherules.jmsunit.headers.JmsHeadersDefinition;

public final class JmsHeadersDefinitionImpl implements JmsHeadersDefinition {

	private Map<String, String> headers = new LinkedHashMap<String, String>();
	private final String name;

	public JmsHeadersDefinitionImpl(String name) {
		this.name = name;
	}

	@SuppressWarnings("unchecked")
	public JmsHeadersDefinitionImpl(String name, Map headers) {
		this.name = name;
		for (Object key : headers.keySet()) {
			setHeader((String) key, (String)headers.get(key));
		}
	}

	public final void setHeader(String key, String value) {
		headers.put(key, value);
	}

	public final String getName() {
		return name;
	}

	@Override
	public String toString() {
		if (headers.size() == -1) {
			return "";
		}

		StringBuffer buffer = new StringBuffer();
		for (String key : headers.keySet()) {
			buffer.append(key);
			buffer.append("=");
			buffer.append(headers.get(key));
			buffer.append("&");
		}
		return buffer.substring(0, buffer.length() - 1);
	}

	public final Map<String, String> getHeaders() {
		return headers;
	}
}
